package com.ccx.coachsite.bus;

import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.task.RegisterTask;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.CoachSiteMainActivity;
import com.ccx.coachsite.MainMenuActivity;
import com.ccx.coachsite.R;
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

public class RegisterBus extends BaseBus
{
	private Account mAccount;

	public RegisterBus(Activity activity) {
		super(activity, activity.getString(R.string.register_running));
		mContext = activity;
		successInfo = "注册成功";
		errorInfo = "注册失败";
	}

	public void setParams(Account account) 
	{
		mAccount = account;
	}
	
	public void onCompleted(boolean isSuccess, String desc) {
		doLoginComplete(isSuccess, desc);
		Intent intent = new Intent(mContext, CoachSiteMainActivity.class);
		mContext.startActivity(intent);
		if (mActivity != null)
			mActivity.finish();

	}

	private void doLoginComplete(boolean isSuccess, String desc) {
		dismissProgress();

		String toaskInfo = successInfo;
		if (!isSuccess) {
			toaskInfo = errorInfo + "," + desc;
		}
		Toast.makeText(mContext, toaskInfo, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void callBack(int taskType, BaseResp response, String responseJson) {
		if (response.isSuccess()) {
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_Mobile_Key, mAccount.getMobile());
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_UserId_Key, mAccount.getUserId()+"");
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_Password_Key, mAccount.getPassword());
			SharedPreferencesUtil.setBoolean(mContext, SharedKey.Shared_Is_Expert, false);
			LoadDataBus loadData = new LoadDataBus(mContext, this);
			loadData.loadData(mAccount.getMobile());
			
		} else {
			doLoginComplete(false, response.getDesc());
		}
	}

	public void register() 
	{
		showProgress();
		RegisterTask task = new RegisterTask(mActivity, this, mAccount);
		task.execute();
	}
}